﻿/*	@file	r_math_vector2.cpp
	@brief	ベクトル
Copyright (c) 2009 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_math_vector2.h"
#include "r_math_def.h"
#include <math.h>

namespace r{
	namespace math{
		Vector2D :: Vector2D() {}
		Vector2D :: Vector2D ( r_f32 sx, r_f32 sy )
		{
			x = sx;
			y = sy;
		}
		Vector2D :: Vector2D ( const r_f32 *sv )
		{
			x = sv[0];
			y = sv[1];
		}

		r_bool		Vector2D :: operator == ( const Vector2D &src ) const
		{
			return ( x == src.x ) && ( y == src.y );
		}

		Vector2D	Vector2D :: operator + ( const Vector2D &src ) const
		{
			Vector2D dst ( x + src.x, y + src.y );
			return dst;
		}

		Vector2D	Vector2D :: operator - ( const Vector2D &src ) const
		{
			Vector2D dst ( x - src.x, y - src.y );
			return dst;
		}

		Vector2D	Vector2D :: operator * ( const Vector2D &src ) const
		{
			Vector2D dst ( x * src.x, y * src.y );
			return dst;
		}

		Vector2D	Vector2D :: operator * ( const r_f32 &src ) const
		{
			Vector2D dst ( x * src, y * src );
			return dst;
		}

		Vector2D	Vector2D :: operator / ( const Vector2D &src ) const
		{
			Vector2D dst ( x / src.x, y / src.y );
			return dst;
		}

		Vector2D	Vector2D :: operator / ( const r_f32 &src ) const
		{
			Vector2D dst ( x / src, y / src );
			return dst;
		}

		Vector2D&	Vector2D :: operator += ( const Vector2D &src )
		{
			x += src.x;
			y += src.y;
			return *this;
		}

		Vector2D&	Vector2D :: operator -= ( const Vector2D &src )
		{
			x -= src.x;
			y -= src.y;
			return *this;
		}

		Vector2D&	Vector2D :: operator *= ( const Vector2D &src )
		{
			x *= src.x;
			y *= src.y;
			return *this;
		}

		Vector2D&	Vector2D :: operator /= ( const Vector2D &src )
		{
			x /= src.x;
			y /= src.y;
			return *this;
		}

		Vector2D&	Vector2D :: operator *= ( const r_f32 &src )
		{
			x *= src;
			y *= src;
			return *this;
		}

		Vector2D&	Vector2D :: operator /= ( const r_f32 &src )
		{
			x /= src;
			y /= src;
			return *this;
		}

		r_f32		Vector2D :: dot ( const Vector2D &src ) const
		{
			return ( x * src.x ) + ( y * src.y );
		}

		r_f32		Vector2D :: cross ( const Vector2D &src ) const
		{
			return ( x * src.y ) - ( y * src.x );
		}

		r_f32		Vector2D :: length ( void ) const
		{
			return sqrt ( dot ( *this ) );
		}

		r_f32		Vector2D :: lengthsq ( void ) const
		{
			return dot ( *this );
		}

		void		Vector2D :: normalize ( void )
		{
			r_f32 len = length();
			if ( r::math::fisZero ( len ) ) {
				x = y = 0;
				return;
			}
			x /= len;
			y /= len;
		}

		Vector2D	Vector2D :: normal ( void ) const
		{
			r_f32 len = length();
			if ( r::math::fisZero ( len ) ) {
				return Vector2D ( 0, 0 );
			}
			return Vector2D ( x / len, y / len );
		}

		void		Vector2D :: zero ( void )
		{
			x = y = 0;
		}

	}
}
